package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.sky.cache.JVMCached;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysDict;
import org.sky.sys.entity.SysDictItem;
import org.sky.sys.service.SysDictService;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysDictController
 * @ProjectName springboot-platform
 * @Description: TODO
 * @date 2019/3/12 16:31
 * @Version 1.0
 */
@Controller
@RequestMapping(value="/sys/SysDict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;
    /**
     * 打开字典页面
     * @return
     */
    @GetMapping(value="/initSysDictMngPage")
    public String initSysDictMngPage(){
        return "sys/dict/sysdictmng";
    }

    /**
     * 字典类型分页查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysDictByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectSysDictByPage(
            HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysDictService.selectSysDictByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }

    /**
     * 字典项分页查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysDictItemByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectSysDictItemByPage(
            HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysDictItem.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysDictService.selectSysDictItemByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }

    /**
     * 打开字典类型增加页面
     * @return
     */
    @GetMapping(value="/initSysDictAddPage")
    public String initSysDictAddPage(){
        return "sys/dict/sysdictedit";
    }
    /**
     * 打开字典类型编辑页面
     * @return
     */
    @GetMapping(value="/initSysDictEditPage")
    public String initSysDictEditPage(){
        return "sys/dict/sysdictedit";
    }

    /**
     * 根据主键查询字典类型
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysDictById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysDictById(
            @PathVariable Long id){
        SysDict bean = sysDictService.getSysDictById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
     * 打开字典项增加页面
     * @return
     */
    @GetMapping(value="/initSysDictItemAddPage")
    public String initSysDictItemAddPage(){
        return "sys/dict/sysdictitemedit";
    }
    /**
     * 打开字典项编辑页面
     * @return
     */
    @GetMapping(value="/initSysDictItemEditPage")
    public String initSysDictItemEditPage(){
        return "sys/dict/sysdictitemedit";
    }

    /**
     * 根据主键查询字典项
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysDictItemById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysDictItemById(
            @PathVariable Long id){
        SysDictItem bean = sysDictService.getSysDictItemById(id);
        return JsonUtils.obj2json(bean);
    }

    /**
     * 删除字典类型
     * @param request
     * @return
     */
    @PostMapping(value = "/delSysDict",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysDict(
            HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List de=JsonUtils.json2list(delRows, SysDict.class);
            sysDictService.delSysDictById(de);
            result = Result.success("删除成功",null,null);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 删除字段项
     * @param request
     * @return
     */
    @PostMapping(value = "/delSysDictItem",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysDictItem(
            HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List de=JsonUtils.json2list(delRows, SysDictItem.class);
            sysDictService.delSysDictItemById(de);
            result = Result.success("删除成功",null,null);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 保存{新增/修改}字典类型
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysDict",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysDict(
            HttpServletRequest request){
        Result result= null;
        try {
            SysDict edit = RequestUtils.getEntityBean(request,SysDict.class);
            sysDictService.saveAddEditSysDict(edit);
            result = Result.success("保存成功",null,null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 保存{新增/修改}字典项
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysDictItem",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysDictItem(
            HttpServletRequest request){
        Result result= null;
        try {
            SysDictItem edit = RequestUtils.getEntityBean(request,SysDictItem.class);
            sysDictService.saveAddEditSysDictItem(edit);
            result = Result.success("保存成功",null,null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 刷新内存中字典项
     * @return
     */
    @PostMapping(value = "/refreshSysDictItemJVMCached",produces = "application/json;charset=UTF-8")
    public @ResponseBody String refreshSysDictItemJVMCached(){
        Result result = null;
        try {
            BspUtils.getBean(JVMCached.class).refreshSysDictJVMCached();
            result = Result.success("刷新成功",null,null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("刷新失败",null,null);
        }
        return JsonUtils.obj2json(result);
    }
}
